<?
   //classe di gestione categorie
   Class topic    {

         var $idtopic;
         var $nome;
         var $idfather;
         var $ordine;
         var $id_user;
         var $lang;

      function topic($idtopic,$nome="",$idfather="",$id_user="")   {
      	global $lang;
        if($idtopic == "")  {
         $this->idtopic = "";
         $res_max = mysql_query("SELECT MAX(ordine) FROM topics WHERE idfather='$idfather' AND lang='$lang';");
         list($maxordine) = mysql_fetch_row($res_max);
		 if(is_null($maxordine))
		  $this->ordine = 0;
		 else 
          $this->ordine = $maxordine + 1;
        }  else  {
         $qry = "SELECT * FROM topics WHERE idtopic='$idtopic';";
         $result = mysql_query($qry);
         list($idtopic, $nome, $idfather, $ordine, $id_user) = mysql_fetch_row($result);
         $this->idtopic = $idtopic;
         $this->ordine = $ordine;
        }
          $this->nome = $nome;
          $this->idfather = $idfather;
          $this->id_user = $id_user;
          $this->lang = $lang;
      }

      function save()  {
        $qry = "INSERT INTO topics VALUES(NULL, '$this->nome', '$this->idfather', '$this->ordine', '$this->id_user','$this->lang');";
        if(mysql_query($qry)) {
         $this->idtopic = mysql_insert_id();
         $error = 0;
        } else {
         $error = "Inserimento errato. Ripetere l'operazione.";
        }
       return $error;
      }

      function update($nome)  {
       $qry = "UPDATE topics SET nome='$nome' WHERE idtopic='$this->idtopic';";
       if(mysql_query($qry)) {
        $this->nome = $nome;
        $error = 0;
       }  else  {
        $error = "Errore nell'operazione.";
       }
       return $error;
      }

      function delete()  {
       //controllo la presenza di sottocategorie o pagine
       $qry_cat = "SELECT idtopic FROM topics WHERE idfather='$this->idtopic';";
       $res_cat = mysql_query($qry_cat);
       $num_cat = mysql_num_rows($res_cat);
       if($num_cat > 0) {
        $error = "Impossibile cancellare la categoria, contiene sottocategorie. E' necessario eliminarle prima!";
        return $error;
       }  else  {
          $qry_page = "SELECT idpage FROM content WHERE idtopic='$this->idtopic';";
          $res_page = mysql_query($qry_page);
          $num_page = mysql_num_rows($res_page);
          if($num_page > 0) {
           $error = "Impossibile cancellare la categoria, contiene pagine. E' necessario eliminarle prima!";
           return $error;
          }
       }
       //procedura di eliminazione
       $qry = "DELETE FROM topics WHERE idtopic='$this->idtopic' AND id_user='$this->id_user';";
       if(mysql_query($qry))  {
        $qry2 = "SELECT idtopic FROM topics WHERE ordine > $this->ordine AND id_user='$this->id_user' AND idfather='$this->idfather';";
        $result2 = mysql_query($qry2);
        while(list($idtopic) = mysql_fetch_row($result2))  {
         mysql_query("UPDATE topic SET ordine=ordine-1 WHERE idtopic='$idtopic' AND id_user='$this->id_user';");
        }
        $error = 0;
       }  else  {
        $error = "Errore nell'operazione.";
       }
       return $error;
      }

      function up()  {
       global $lang;
	   $ordineup = $this->ordine-1;
       $qryup = "UPDATE topics SET ordine=ordine+1 WHERE ordine='$ordineup' AND id_user='$this->id_user' AND idfather='$this->idfather' AND lang='$lang';";
       if (mysql_query($qryup)) {
        $qry = "UPDATE topics SET ordine=ordine-1 WHERE ordine='$this->ordine' AND id_user='$this->id_user' AND idtopic='$this->idtopic' AND lang='$lang';";
        if(mysql_query($qry)) {
          $error = 0;
        }  else  {
          $error = "Errore nell'operazione.";
        }
       }  else
         $error = "Errore nell'operazione.";
       return $error;
      }

      function down()  {
       global $lang;
	   $ordinedown = $this->ordine + 1;
       $qrydown = "UPDATE topics SET ordine=ordine-1 WHERE ordine='$ordinedown' AND id_user='$this->id_user' AND idfather='$this->idfather' AND lang='$lang';";
       if (mysql_query($qrydown)) {
        $qry = "UPDATE topics SET ordine=ordine+1 WHERE ordine='$this->ordine' AND id_user='$this->id_user' AND idtopic='$this->idtopic' AND lang='$lang';";
        if(mysql_query($qry)) {
          $error = 0;
        }  else  {
          $error = "Errore nell'operazione.";
        }
       }  else
         $error = "Errore nell'operazione.";
       return $error;
      }
   }

   //classe di gestione pagine
   Class content  {

         var $idpage;
         var $idtopic;
         var $titolo;
         var $contenuto;
         var $tagtitle;
         var $tagkey;
         var $tagdesc;
         var $metatitle;
         var $metakey;
         var $metadesc;
         var $hidden;
         var $ordine;
         var $id_user;
		 var $pubblica;
		 var $lang;

      function content($idpage, $idtopic="", $titolo="", $contenuto="", $tagtitle="", $tagkey="", $tagdesc="", $metatitle="", $metakey="", $metadesc="", $hidden="", $id_user="")  {
      	global $lang;
        if($idpage == "")  {
         $this->idpage = "";
         $res_max = mysql_query("SELECT MAX(ordine) FROM content WHERE idtopic='$idtopic' AND lang='$lang';");
         list($maxordine) = mysql_fetch_row($res_max);
		 if(is_null($maxordine))
		  $this->ordine = 0;
		 else 
          $this->ordine = $maxordine + 1;
        }  else  {
         $qry = "SELECT * FROM content WHERE idpage='$idpage';";
         $result = mysql_query($qry);
         list($idpage, $idtopic, $titolo, $contenuto, $tagtitle, $tagkey, $tagdesc, $metatitle, $metakey, $metadesc, $hidden, $ordine, $id_user, $pubblica) = mysql_fetch_row($result);
         $this->idpage = $idpage;
         $this->ordine = $ordine;
		 $this->pubblica = $pubblica;
        }
         $this->idtopic = $idtopic;
         $this->titolo = $titolo;
         $this->contenuto = $contenuto;
         $this->tagtitle = $tagtitle;
         $this->tagkey = $tagkey;
         $this->tagdesc = $tagdesc;
         $this->metatitle = $metatitle;
         $this->metakey = $metakey;
         $this->metadesc = $metadesc;
         $this->hidden = $hidden;
		 $this->id_user = $id_user;
		 $this->lang = $lang;
      }

      function save()  {
        $qry = "INSERT INTO content VALUES(NULL, '$this->idtopic', '$this->titolo', '$this->contenuto', '$this->tagtitle','$this->tagkey','$this->tagdesc','$this->metatitle','$this->metakey','$this->metadesc','$this->hidden', '$this->ordine', '$this->id_user','y','$this->lang');";
        if(mysql_query($qry)) {
         $this->idpage = mysql_insert_id();
         $error = 0;
        } else {
         $error = "Inserimento errato. Ripetere l'operazione.";
        }
       return $error;
      }

      function update($titolo, $idtopic, $contenuto, $tagtitle, $tagkey, $tagdesc, $metatitle, $metakey, $metadesc, $hidden)  {
       $qry = "UPDATE content SET titolo='$titolo', idtopic='$idtopic', contenuto='$contenuto', tagtitle='$tagtitle', tagkey='$tagkey', tagdesc='$tagdesc', metatitle='$metatitle', metakey='$metakey',metadesc='$metadesc',hidden='$hidden' WHERE idpage='$this->idpage';";
       if(mysql_query($qry)) {
        $this->titolo = $titolo;
        $this->idtopic = $idtopic;
        $this->contenuto = $contenuto;
        $this->tagtitle = $tagtitle;
        $this->tagkey = $tagkey;
        $this->tagdesc = $tagdesc;
        $this->metatitle = $metatitle;
        $this->metakey = $metakey;
        $this->metadesc = $metadesc;
        $this->hidden = $hidden;
        $error = 0;
       }  else  {
        $error = "Errore nell'operazione.";
       }
       return $error;
      }

      function delete()  {
       $qry = "DELETE FROM content WHERE idpage='$this->idpage' AND id_user='$this->id_user';";
       if(mysql_query($qry))  {
        $qry2 = "SELECT idpage FROM content WHERE ordine > $this->ordine AND id_user='$this->id_user' AND idtopic='$this->idtopic';";
        $result2 = mysql_query($qry2);
        while(list($idpage) = mysql_fetch_row($result2))  {
         mysql_query("UPDATE content SET ordine=ordine-1 WHERE idpage='$idpage' AND id_user='$this->id_user'");
        }
        $error = 0;
       }  else  {
        $error = "Errore nell'operazione.";
       }
       return $error;
      }

      function up()  {
	   $ordineup = $this->ordine-1;
       $qryup = "UPDATE content SET ordine=ordine+1 WHERE ordine='$ordineup' AND id_user='$this->id_user' AND idtopic='$this->idtopic' AND lang='$lang';";
       if (mysql_query($qryup)) {
        $qry = "UPDATE content SET ordine=ordine-1 WHERE ordine='$this->ordine' AND id_user='$this->id_user' AND idpage='$this->idpage' AND lang='$lang';";
        if(mysql_query($qry)) {
          $error = 0;
        }  else  {
          $error = "Errore nell'operazione.";
        }
       }  else
         $error = "Errore nell'operazione.";
       return $error;
      }

      function down()  {
	   $ordinedown = $this->ordine + 1;
       $qryup = "UPDATE content SET ordine=ordine-1 WHERE ordine='$ordinedown' AND id_user='$this->id_user' AND idtopic='$this->idtopic' AND lang='$lang';";
       if (mysql_query($qryup)) {
        $qry = "UPDATE content SET ordine=ordine+1 WHERE ordine='$this->ordine' AND id_user='$this->id_user' AND idpage='$this->idpage' AND lang='$lang';";
        if(mysql_query($qry)) {
          $error = 0;
        }  else  {
          $error = "Errore nell'operazione.";
        }
       }  else
         $error = "Errore nell'operazione.";
       return $error;
      }
	  
      function pubblica()  {
	    //funzione che rende attiva la pagina
		$qry = "UPDATE content SET pubblica='y' WHERE idpage='$this->idpage';";
        if(mysql_query($qry)) {
          $error = 0;
        }  else  {
          $error = "Errore nell'operazione.";
        }
		return $error;		  
	  }

      function disattiva()  {
	    //funzione che rende disattiva la pagina
		$qry = "UPDATE content SET pubblica='n' WHERE idpage='$this->idpage';";
        if(mysql_query($qry)) {
          $error = 0;
        }  else  {
          $error = "Errore nell'operazione.";
        }		  
		return $error;		  
	  }
	  
	  function getMenu($idtopic)  {
	    global $conn;
		$qry = "SELECT idpage,titolo FROM content WHERE idtopic=$idtopic AND pubblica='y' ORDER BY ordine;";
		if($idtopic==8)
		  $rel = 'rel="nofollow"';
		else
		  $rel = "";
		$rs = $conn->Execute($qry);
		if($rs->RecordCount()>0)  {
		  $menu = '<ul>';
          while(!$rs->EOF)  {
          	$menu .= '<li><a href="/webpage.php?idpage='.$rs->Fields("idpage").'" '.$rel.'>'.$rs->Fields("titolo").'</a></li>';
		    $rs->MoveNext();	
		  }
		  if($idtopic==10)
		    $menu .= '<li><a href="/webpage.php?idpage=4">Affiliazione</a></li>
	<li><a href="/feedback.php">Testimonianze dei nostri clienti</a></li>
    <li><a href="/sitemap.php">Mappa Sito</a></li>';
          $menu .= '</ul>';
		  $rs->Close();
		}
		return $menu;	
	  }
	  
	  function getMenuMap()  {
	    global $conn;
		$qry = "SELECT idpage,titolo FROM content WHERE pubblica='y' AND lang='ita' AND idtopic IN(6,9,10,11) ORDER BY ordine;";
		$rs = $conn->Execute($qry);
		if($rs->RecordCount()>0)  {
		  $menu = '';
          while(!$rs->EOF)  {
          	$menu .= '<p><a class="tit02" href="/webpage.php?idpage='.$rs->Fields("idpage").'">'.$rs->Fields("titolo").'</a></p>';
		    $rs->MoveNext();	
		  }
		  $rs->Close();
		}
		return $menu;	
	  }
   }

	//classe di gestione menu a sx
	class menusx  {
	  
	  var $class;
	  var $result;
	  var $html;
	  var $alto;
	  
	  function menusx()  {
	    global $lang;
	    $qry = "SELECT idtopic,nome FROM topics WHERE idfather='0' AND lang='$lang' ORDER BY ordine ASC;";
	    $result = mysql_query($qry);
	    $this->class = $class;
	    $this->result = $result;
	    $this->html = "";
		$this->alto = "";	    
	}
	  function show()   {
	    global $lang;
	    $num = mysql_num_rows($this->result);
	    if($num > 0)  {
		  $this->html .= "";
		  $i = 0;
		  while(list($idtopic, $nome) = mysql_fetch_row($this->result)) {
#################################### categorie principali #######################
		    $this->html .= "<tr><td class=alto>$nome</td></tr>\n";
#################################### sottocategorie #############################
			    $qry = "SELECT idtopic,nome FROM topics WHERE idfather='$idtopic' AND lang='$lang' ORDER BY ordine ASC;";
			    $res_sot = mysql_query($qry);
			    $num_sot = mysql_num_rows($res_sot);
			      $this->html .= "<tr>
		                      <td><ul class=prod>";
##################################################################################
			    if($num_sot > 0)  {
				  while(list($idtopic_s, $nome_s) = mysql_fetch_row($res_sot)){
				    $this->html .= "<li><a href=\"webpage.php?idtopic=$idtopic_s\">$nome_s</a></li>\n";
				  }
				}
################################ pagine ##########################################
				$qry_p = "SELECT idpage,titolo FROM content WHERE idtopic='$idtopic' AND pubblica='y' ORDER BY ordine ASC;";
				$res_p = mysql_query($qry_p);
				$num_p = mysql_num_rows($res_p);
				if($num_p > 0)  {
				  $i = 1;
				      while(list($idpage,$titolo) = mysql_fetch_row($res_p))  {
				          $titlink = ereg_replace(" ","_",$titolo);
						  $this->html .= "<li><a href=\"".$tablelink."webpage.php?idpage=$idpage&idtopic=$idtopic&title=$titlink\">$titolo</a></li>\n";
		              }                		  
				}		
						  $this->html .= "</ul>
						  				</td>
									</tr>";  
##################################### end ########################################
		  }
		} 
	}	
}

	//classe di gestione Navbar
	class navBar  {
	  
	  var $class;
	  var $html;
	  
	  function navBar($idtopic,$class,$idpage)  {
	    $newTopic = new topic($idtopic);
	    $this->class = $class;
	    $this->html = getparent($idtopic, $newTopic->nome, $class, $idpage);
	  }
	}
	
	//classe di gestione sottocategorie
	class section  {
	  
	  var $class;
	  var $html;
	  
	  function section($idtopic,$id_user,$class)  {
	  	global $lang;
	    $qry = "SELECT idtopic,nome FROM topics WHERE idfather='$idtopic' AND lang='$lang' ORDER BY ordine ASC;";
	    $result = mysql_query($qry);
	    $this->class = $class;
	    $this->html = "";
	    while(list($idtopics,$nome) = mysql_fetch_row($result))  {
		  $this->html .= "<a class=\"$this->class\" href=\"webpage.php?idtopic=$idtopics\">$nome</a>\n";
		}
	}
	
	function showAll($class1, $class2, $id_user)  {
	  global $lang;
	    $qry = "SELECT idtopic,nome FROM topics WHERE idfather='0' AND lang='$lang' ORDER BY ordine ASC;";
	    $result = mysql_query($qry);
	    $num = mysql_num_rows($result);
	    $this->html = "<tr><td valign=top>";
	    $i = 1;
	    while(list($idtopic,$nome) = mysql_fetch_row($result))  {
	  		$this->html .= "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
                              <tr>
                                <td><img src=\"images/spacer10.gif\" width=\"14\" height=\"14\"></td>
                              </tr>
                            </table>
							<table width=\"100%\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\" id=\"$class1\">
                                <tr>
                                  <td>$nome</td>
                                </tr>
                              </table>\n";
	  		$this->html .= "<table width=\"100%\" border=\"0\" cellpadding=\"5\" cellspacing=\"0\" id=\"$class2\">
                                <tr>
                                  <td>";
	  		//elenco sub-categories
	  		$subTopic = new section($idtopic,$id_user,$class2);
	  		$this->html .= $subTopic->html;
	  		//elenco pagine
	  		$subPage = new page($idtopic,$id_user,$class2);
	  		$this->html .= $subPage->html;
	  		$this->html .= "</td>
                                </tr>
                              </table>";
            $resto = $i % 2;
            if($resto == 0)
              $this->html .= "</td></tr><tr><td valign=top>";
            else
			  $this->html .= "</td><td valign=top>";
			$i++;    
		}
		$this->html .= "</td></tr>\n";
	}
}

	//classe di gestione pagine
	class page  {
	  
	  var $class;
	  var $html;
	  
	  function page($idtopic,$id_user,$class)  {
	    $qry = "SELECT idpage,titolo FROM content WHERE idtopic='$idtopic' AND pubblica='y' ORDER BY ordine ASC;";
	    $result = mysql_query($qry);
	    $this->class = $class;
	    $this->html = "";
	    while(list($idpage,$titolo) = mysql_fetch_row($result))  {
		  $this->html .= "<a class=\"$this->class\" href=\"webpage.php?idpage=$idpage&idtopic=$idtopic\">$titolo</a>\n";
		}
	}
}
?>